package com.xayy.health.ds.controller;

import cn.hutool.core.util.StrUtil;
import cn.kawins.base.result.ResultBody;
import com.xayy.health.ds.model.DsSatelliteParam;
import com.xayy.health.ds.service.DsSatelliteParamService;
import com.xtck.ha.common.core.constant.HttpStatus;
import com.xtck.ha.common.core.utils.poi.ExcelUtil;
import com.xtck.ha.common.core.web.controller.BaseController;
import com.xtck.ha.common.core.web.domain.AjaxResult;
import com.xtck.ha.common.core.web.page.TableDataInfo;
import com.xayy.health.ds.model.DsSatelliteInfo;
import com.xayy.health.ds.service.DsSatelliteInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 模型列表 controller
 *
 * @ClassName: DsSatelliteInfoController
 * @Description: 外部访问接口
 * @author liuyuan
 * @date 2025-07-08
 */
@Api(tags = "卫星信息表管理")
@RestController
@RequestMapping("/health-mgr-back/v1.0/satellite")
public class DsSatelliteInfoController extends BaseController {

    @Autowired
    private DsSatelliteInfoService dsSatelliteInfoService;
    @Autowired
    private DsSatelliteParamService dsSatelliteParamService;


    @ApiOperation("分页查询卫星信息表列表")
    @GetMapping("/page")
    public AjaxResult page(DsSatelliteInfo dsSatelliteInfo)
    {
        startPage();
        List<DsSatelliteInfo> list = dsSatelliteInfoService.selectTList(dsSatelliteInfo);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setList(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(dsSatelliteInfoService.count(dsSatelliteInfo));
        return AjaxResult.success(rspData);
    }

    @ApiOperation("列表查询卫星信息表")
    @GetMapping("/list")
    public List<DsSatelliteInfo> list(DsSatelliteInfo dsSatelliteInfo)
    {
        List<DsSatelliteInfo> list = dsSatelliteInfoService.selectTList(dsSatelliteInfo);
        return list;
    }

    @ApiOperation("导出卫星信息表列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DsSatelliteInfo dsSatelliteInfo)
    {
        List<DsSatelliteInfo> list = dsSatelliteInfoService.selectTList(dsSatelliteInfo);
        ExcelUtil<DsSatelliteInfo> util = new ExcelUtil<>(DsSatelliteInfo.class);
        util.exportExcel(response, list, "样例数据");
    }

    @ApiOperation("获取$卫星信息表详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dsSatelliteInfoService.selectTById(id));
    }

    @ApiOperation("新增$卫星信息表")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DsSatelliteInfo dsSatelliteInfo)
    {
        return toAjax(dsSatelliteInfoService.insertT(dsSatelliteInfo));
    }

    @ApiOperation("修改卫星信息表")
    @PutMapping
    public AjaxResult update(@RequestBody DsSatelliteInfo dsSatelliteInfo)
    {
        int i = dsSatelliteInfoService.updateT(dsSatelliteInfo);
        return toAjax(i);
    }

    @ApiOperation("删除卫星信息表")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dsSatelliteInfoService.deleteTByIds(ids));
    }


    /**
     * 筛选遥测参数
     * @param satelliteId 卫星id
     * @param keyword 关键字
     * @return ResultBody
     */
    @ApiOperation(value = "筛选遥测参数")
    @GetMapping("screenParam/{satelliteId}")
    public ResultBody screenParam(@PathVariable Long satelliteId, @RequestParam(required = false) String keyword, @RequestParam(required = false) String searchKey) {
        List<DsSatelliteParam> satelliteParams = dsSatelliteParamService.selectByOther(satelliteId);
        if (StrUtil.isNotBlank(keyword)) {
            satelliteParams = satelliteParams.stream()
                    .filter(x -> StrUtil.isNotBlank(x.getParamName()) && StrUtil.isNotBlank(x.getTelemetryCode()))
                    .filter(x -> x.getParamName().toLowerCase().contains(keyword.toLowerCase()) || x.getTelemetryCode().toLowerCase().contains(keyword.toLowerCase()))
                    .collect(Collectors.toList());
        }

        if(StrUtil.isNotBlank(searchKey)){
            satelliteParams=satelliteParams.stream().filter(x ->x.getParamName().contains(searchKey)).collect(Collectors.toList());
        }
        return ResultBody.success(satelliteParams);
    }

    /**
     * 获取节点树左侧信第三方接口
     * @param pageNum  页码
     * @param pageSize 页数
     * @param xingType 卫星类型
     * @return 结果日志
     */
    @ApiOperation(value = "树结构(第三方接口)")
    @GetMapping("getLeftTreeByOther")
    public ResultBody getLeftTreeByOther(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                         @RequestParam String xingType) {
        return dsSatelliteInfoService.getLeftTreeByOther(pageNum, pageSize, xingType).body();
    }



}
